package timeToLive;

import com.atguigu.Main;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * Created by 小可乐$ on 2024/9/11$ 20:41$
 * description: 消息超时:队列设置存活时间和代码设置时间，以最短时间为主
 */
@SpringBootTest(classes = Main.class)
public class RabbitMQTime {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public static final String EXCHANGE_DIRECT="exchange.direct.order";
    public static final String ROUTING_KEY="stock";
    /***
     *
     */
    @Test
    public void Test(){
        // 1、创建消息后置处理器对象
        MessagePostProcessor messagePostProcessor = (Message message) -> {

            // 设定 TTL 时间，以毫秒为单位，5s
            message.getMessageProperties().setExpiration("5000");

            return message;
        };

        // 2、发送消息
        rabbitTemplate.convertAndSend(
                EXCHANGE_DIRECT,
                ROUTING_KEY,
                "Hello atguigu", messagePostProcessor);
    }
}
